package com.aptemo.webetek.client.widgets.players;

import java.util.ArrayList;
import java.util.List;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.GotAsynchCallback;
import com.aptemo.webetek.client.commands.impl.GetPlayersAction;
import com.aptemo.webetek.client.commands.impl.InvitationAction;
import com.aptemo.webetek.client.commands.responses.GotPlayersList;
import com.aptemo.webetek.client.enums.EnumUserRole;
import com.aptemo.webetek.client.enums.EnumUserStatus;
import com.aptemo.webetek.client.events.DecisionEvent;
import com.aptemo.webetek.client.events.DecisionHandler;
import com.aptemo.webetek.client.events.SelectedPlayerEvent;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.widgets.paging.PagingController;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasWidgets;

public class PlayersPresenter
		extends PagingController<UserBean>
{

	private PlayersDisplay			display;
	private WebetekUserServiceAsync	rpcService;
	private String					teamId;
	private ArrayList<UserBean>		players;
	private HandlerManager			eventbus;
	private int						pageNo;
	protected EnumUserStatus		status;

	public PlayersPresenter(PlayersDisplay view, HandlerManager eventBus, WebetekUserServiceAsync rpcService, String id, int pageNo)
	{
		super(view.getPagingView(), 5);

		this.display = view;
		this.rpcService = rpcService;
		this.teamId = id;
		this.eventbus = eventBus;
		this.pageNo = pageNo;
	}

	@Override
	public void go(HasWidgets container)
	{
		bind();

		container.clear();
		container.add(display.getDisplay());

		// 
		loadUsers();
	}

	private void loadUsers()
	{
		display.getInfoField().setText(display.translate("loading"));

		// GetUsersAction action = new GetUsersAction(EnumUserStatus.CREATED);
		GetPlayersAction action = new GetPlayersAction(this.teamId);
		rpcService.invoke(action, new GotPlayersList()
		{

			@Override
			public void onError(String message)
			{
				display.getInfoField().setText(message);
			}

			@Override
			public void got(ArrayList<UserBean> list)
			{
				display.getInfoField().setText("");

				players = list;
				// display.setData(list);
				renderList(list);

			}
		});

	}

	private void bind()
	{
		// display.setPagingView(super.getDisplay());

		display.getList().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				int selectedRow = display.getClickedRow(event);

				if (selectedRow >= 0)
				{
					// get it from paging...
					UserBean bet = getItem(selectedRow);
					String id = bet.getKey();
					eventbus.fireEvent(new SelectedPlayerEvent(id, teamId));
				}

			}
		});

		display.getStatusListHandler().addChangeHandler(new ChangeHandler()
		{

			@Override
			public void onChange(ChangeEvent event)
			{
				status = display.getStatusFilterValue();
				renderList(players);
			}
		});

		display.getInvitationConfirmation().addDecisionHandler(new DecisionHandler()
		{

			@Override
			public void onConfirmation(DecisionEvent event)
			{
				display.getInfoField().setText(display.translate("sendingMessage"));

				// get fields...
				String email = display.getInvitationEmailField().getText();
				String message = display.getInvitationMessageField().getText();
				String subject = display.getInvitationSubjectField().getText();
				// invitation rpc
				InvitationAction action = new InvitationAction(teamId, email, message, subject, display.translate("farewell"));
				rpcService.invoke(action, new GotAsynchCallback<UserBean>()
				{

					@Override
					public void onError(String message, EErrorCodes code)
					{
						display.getInfoField().setText(message);
					}

					@Override
					public void onSuccess(UserBean result)
					{
						display.getInfoField().setText(display.translate("invitationSucceded"));

						loadUsers();
					}
				});
			}
		});
	}

	protected void renderList(ArrayList<UserBean> players)
	{
		display.getPagingView().setVisible(true);

		List<UserBean> arr = new ArrayList<UserBean>();

		if (players != null)
		{
			for (UserBean player : players)
			{
				if (status == null || status.equals(player.getStatus()))
				{
					arr.add(player);
				}
				if ((player.getRole() == EnumUserRole.EDITOR) || (player.getRole() == EnumUserRole.ADMIN))
				{
					// add owner to invitation message...
					String msg = display.getInvitationMessageField().getText();
					msg = player.getEmail() + " " + msg;
					display.getInvitationMessageField().setText(msg);
				}

			}
		}
		setList(arr, pageNo);
	}

	@Override
	public void refreshPaging(List<UserBean> data)
	{
		display.setData(data);
	}

}
